﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL.FunctionControl
{
	public partial class UserMngUserControl : BaseUserControl
	{
		#region Constructor
		public UserMngUserControl()
		{
			InitializeComponent();
			UpdateGridView();
		}
		#endregion

		#region Event Handler
		private void btnUpdate_Click(object sender, EventArgs e)
		{
			int nSelectRow = CheckChoose();
			if (nSelectRow < 0)
			{
				return;
			}
			UserEditForm userMngForm = new UserEditForm(true);
			userMngForm.UserInfo = buildUserInfo(nSelectRow);
			userMngForm.ShowDialog(this);
			if (userMngForm.DialogResult == DialogResult.OK)
			{
				UpdateGridView();
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			UpdateGridView();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			UserEditForm userMngForm = new UserEditForm(false);
			userMngForm.ShowDialog(this);
			if (userMngForm.DialogResult == DialogResult.OK)
			{
				UpdateGridView();
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			int nSelectRow = CheckChoose();
			if (nSelectRow < 0)
			{
				return;
			}

			if (DeleteGoods(buildUserInfo(nSelectRow)))
			{
				MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				UpdateGridView();
			}
			else
			{
				MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}
		#endregion

		#region Private Method
		private bool DeleteGoods(User user)
		{
			bool bReturn = false;
			UserBL userMng = new UserBL();
			try
			{
				bReturn = userMng.DeleteUser(user) > 0 ? true : false;
			}
			catch
			{

			}

			return bReturn;
		}

		private void UpdateGridView()
		{
			int nResult;
			try
			{
				User userInfo = new User();
				userInfo.Name = txtName.Text.Trim();

				UserBL userBL = new UserBL();
				nResult = userBL.Search(ref userInfo);

				if (userInfo.UserCollection != null)
				{
					dgvGoods.AutoGenerateColumns = false;
					dgvGoods.DataSource = userInfo.UserCollection;
				}				
			}
			catch
			{

			}
		}

		private int CheckChoose()
		{
			int nCount = 0;
			int nRowIndex = -1;
			foreach (DataGridViewRow dgr in dgvGoods.Rows)
			{
				if (dgr.Cells["choose"].Value != null && dgr.Cells["choose"].Value.Equals(true))
				{
					nCount++;
					nRowIndex = dgr.Index;
				}
			}

			if (nCount == 0)
			{
				MessageBox.Show("请选择用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				nRowIndex = -1;
			}

			if (nCount > 1)
			{
				MessageBox.Show("只能选择一个", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				nRowIndex = -1;
			}

			return nRowIndex;
		}

		private User buildUserInfo(int rowIndex)
		{
			User user = new User();
			user.ID = dgvGoods.Rows[rowIndex].Cells["ID"].Value.ToString();
			user.Name = dgvGoods.Rows[rowIndex].Cells["USERNAME"].Value.ToString();
			user.Password = dgvGoods.Rows[rowIndex].Cells["PASSWORD"].Value.ToString();
			return user;
		}
		#endregion
	}
}
